package com.uls.web.controller.czj;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.czj.domain.CzjBuilding;
import com.uls.czj.domain.CzjResidentialproperty;
import com.uls.czj.service.ICzjBuildingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.czj.domain.CzjTemporaryapartment;
import com.uls.czj.service.ICzjTemporaryapartmentService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 周转公寓管理Controller
 * 
 * @author czj
 * @date 2024-11-10
 */
@RestController
@RequestMapping("/czj/temporaryapartment")
public class CzjTemporaryapartmentController extends BaseController
{
    @Autowired
    private ICzjTemporaryapartmentService czjTemporaryapartmentService;
    @Autowired
    private ICzjBuildingService czjBuildingService;
    /**
     * 查询周转公寓管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:temporaryapartment:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzjTemporaryapartment czjTemporaryapartment)
    {
        startPage();
        List<CzjTemporaryapartment> list = czjTemporaryapartmentService.selectCzjTemporaryapartmentList(czjTemporaryapartment);
        for (CzjTemporaryapartment czjTemporaryapartment1:list){
            CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(czjTemporaryapartment1.getBuildingid());
            czjTemporaryapartment1.setBuilding(czjBuilding.getLocation()+czjBuilding.getBuildingname());
        }
        return getDataTable(list);
    }

    /**
     * 导出周转公寓管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:temporaryapartment:export')")
    @Log(title = "周转公寓管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzjTemporaryapartment czjTemporaryapartment)
    {
        List<CzjTemporaryapartment> list = czjTemporaryapartmentService.selectCzjTemporaryapartmentList(czjTemporaryapartment);
        ExcelUtil<CzjTemporaryapartment> util = new ExcelUtil<CzjTemporaryapartment>(CzjTemporaryapartment.class);
        util.exportExcel(response, list, "周转公寓管理数据");
    }

    /**
     * 获取周转公寓管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('czj:temporaryapartment:query')")
    @GetMapping(value = "/{apartmentid}")
    public AjaxResult getInfo(@PathVariable("apartmentid") Long apartmentid)
    {
        return success(czjTemporaryapartmentService.selectCzjTemporaryapartmentByApartmentid(apartmentid));
    }

    /**
     * 新增周转公寓管理
     */
    @PreAuthorize("@ss.hasPermi('czj:temporaryapartment:add')")
    @Log(title = "周转公寓管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzjTemporaryapartment czjTemporaryapartment)
    {
        CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(czjTemporaryapartment.getBuildingid());
        czjBuilding.setBuildingtype("4");
        czjBuildingService.updateCzjBuilding(czjBuilding);
        return toAjax(czjTemporaryapartmentService.insertCzjTemporaryapartment(czjTemporaryapartment));
    }

    /**
     * 修改周转公寓管理
     */
    @PreAuthorize("@ss.hasPermi('czj:temporaryapartment:edit')")
    @Log(title = "周转公寓管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzjTemporaryapartment czjTemporaryapartment)
    {
        return toAjax(czjTemporaryapartmentService.updateCzjTemporaryapartment(czjTemporaryapartment));
    }

    /**
     * 删除周转公寓管理
     */
    @PreAuthorize("@ss.hasPermi('czj:temporaryapartment:remove')")
    @Log(title = "周转公寓管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{apartmentids}")
    public AjaxResult remove(@PathVariable Long[] apartmentids)
    {
        for (Long l:apartmentids){
            Long id= czjTemporaryapartmentService.selectCzjTemporaryapartmentByApartmentid(l).getBuildingid();
            CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(id);
            czjBuilding.setBuildingtype("0");
            czjBuildingService.updateCzjBuilding(czjBuilding);
        }
        return toAjax(czjTemporaryapartmentService.deleteCzjTemporaryapartmentByApartmentids(apartmentids));
    }
}
